/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_jolt_mar_get_int.c                               */
/*  NOTE      =                                                    */
/*  DATE      = 2016/03/07 by songbb                               */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "frct_comm.h"

#include "../../../src/module/tuxedojolt/config.h"
#include "../test_jolt_comm.h"
#include <tuxedojolt/jolt_core.h>
#include <tuxedojolt/jolt_comm.h>
#include <tuxedojolt/jolt_packet.h>
#include <apmtest_comm.h>
#include <tuxedojolt/jolt_mar.h>

int __jolt_mar_detect_jolti18nflag(L7session_jolt *p_st_jolt_sess
                                    , unsigned int ui_msgparam
                                    , L7_jolt_packet *p_st_jolt_packet);

static int __suite_init(void)
{
    return 0;
}

static int __suite_cleanup(void)
{
    return 0;
}

static void __case1(void)
{
    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(test_jolt_create_ct(&p_st_conn, TEST_JOLT_SRC_IP, TEST_JOLT_DST_IP, TEST_JOLT_SRC_PORT, TEST_JOLT_DST_PORT), FR_SUCC);

    L7_session *p_st_session = frct_get_session(p_st_conn);

    L7session_jolt_private *p_st_session_private = (L7session_jolt_private *) frct_get_session_private(p_st_session);
    L7session_jolt *p_st_sess = p_st_session_private->p_st_sess;
    L7_jolt_packet *p_st_jolt_packet = p_st_session_private->p_st_jolt_packet;

    char *p_bin = (char *) "4a4f4c540000000f00000082000000000000008c00000042000000030000000000000001000000000000000000000000000000000000000000000000000000000000019202000014000004b00a00000d000000000b557365722e3040353030320a00000e0000000007776562757365720a00000f00000000000a0000100000000000";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    unsigned int ui_payload_len = strlen(p_bin) / 2;
    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_JOLT_SRC_IP, TEST_JOLT_DST_IP, TEST_JOLT_SRC_PORT, TEST_JOLT_DST_PORT, st_timestamp), FR_SUCC);
    p_st_packet->i_direction = FR_DIRECT_CLIENT2SRV;

    CU_ASSERT_EQUAL(jolt_packet_unfragment(p_st_sess, p_st_packet, p_st_jolt_packet), APM_JOLT_CHECK_FRAGMENT_SUCCESS);

    CU_ASSERT_EQUAL(bytebuff_setPosition(p_st_jolt_packet->p_st_packet, 80), FR_SUCC);

    CU_ASSERT_EQUAL(__jolt_mar_detect_jolti18nflag(p_st_sess, 0xA00000D, p_st_jolt_packet), FR_SUCC);
    CU_ASSERT_EQUAL(p_st_sess->i_jolti18nflag, FR_NO);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);

    test_jolt_destroy_ct(&p_st_conn);
    CU_ASSERT_PTR_NULL(p_st_conn);
}

int add_suite__jolt_mar_detect_jolti18nflag(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" __jolt_mar_detect_jolti18nflag()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", __case1) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
